﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;

using LIM.controller;
using LIM.model;

namespace LIM.forms
{
    public class BaseUserControl : UserControl
    {
        public List<object> ControlPermissionList = new List<object>();

        protected override void OnLoad(EventArgs e)
        {
            if (InitUserInfos.IsAdmin) // check for user is Admin 
            {
                base.OnLoad(e);
                return;
            }

            FormPermissionsInfo FormPermissionsInfo = InitUserInfos.FormPermissions.Find(f => f.Name == this.Name);
            if (FormPermissionsInfo == null)
            {                
                this.Visible = false;
            }
            else
            {
                foreach (var item in ControlPermissionList)
                {
                    if (item is RadMenuItem)
                    {
                        RadMenuItem radItem = (RadMenuItem)item;
                        ControlPermissionsInfo ControlPermissionsInfo = FormPermissionsInfo.Controls.Find(c => c.Name == radItem.Name);
                        if (ControlPermissionsInfo != null)
                            radItem.Enabled = ControlPermissionsInfo.IsEnabled;
                        else
                            radItem.Visibility = ElementVisibility.Collapsed;

                    }
                    else if (item is RadButton)
                    {
                        RadButton radItem = (RadButton)item;
                        ControlPermissionsInfo ControlPermissionsInfo = FormPermissionsInfo.Controls.Find(c => c.Name == radItem.Name);
                        if (ControlPermissionsInfo != null)
                            radItem.Enabled = ControlPermissionsInfo.IsEnabled;
                        else
                            radItem.Visible = false;
                    }
                }
                base.OnLoad(e);
            }            
        }
    }
}
